Update and Add Data

Update Record Data

Description
This example demonstrates how to write a reusable function that programmatically edits/updates an existing database record. Specifically, this example updates a Products record. The reusable function can be called from any page. Although the function is trivial, the purpose is to demonstrate how to programmatically update database records. The example code could be easily modified to update multiple records returned by a filtered query, etc.
Variables
Table Name
Select the name of the database table.
Applies to
Page class
Code
 
/// 
/// This method updates a record in the table.
/// 
public static void UpdateRecord() 
{ 
    // Get an existing record with ID 1.Set second parameter to true
    // in GetRecord() to retrieve an updatable record.
   ${${Table Name}RecordClassName} rec = ${${Table Name}ClassName}.GetRecord("1", true );   

    // Update the fields of the record as shown below.
    // EmployeesRecord.LastName = "LastName";
    // EmployeesRecord.FirstName = "FirstName";

    // Save the update.
    rec.Save();

    // Example shown below shows the transaction handling. 
    // Note that you need to start a transaction before calling
    // UpdateRecord() function.

    // try
    // {
    //   DbUtils.StartTransaction();
    //   UpdateRecord();
    //   DbUtils.CommitTransaction();
    //   this.${Table Name}TableControl.DataChanged = true;
    // }
    // catch(Exception ex)
    // {
    //   DbUtils.RollBackTransaction();
    //   BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "UNIQUE_SCRIPTKEY", ex.Message);
    // }
    // finally
    // {
    //    DbUtils.EndTransaction();
    // } 
}
     

Terms of Service Privacy Statement